home *** CD-ROM | disk | FTP | other *** search
Wrap
package com.sun.java.swing.plaf.basic; import com.sun.java.swing.AbstractButton; import com.sun.java.swing.ButtonModel; import com.sun.java.swing.Icon; import com.sun.java.swing.JComponent; import com.sun.java.swing.JMenu; import com.sun.java.swing.JMenuBar; import com.sun.java.swing.JMenuItem; import com.sun.java.swing.KeyStroke; import com.sun.java.swing.SwingUtilities; import com.sun.java.swing.UIManager; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.KeyEvent; public class BasicGraphicsUtils { public static final Font controlFont = new Font("Dialog", 0, 12); public static Color control; public static Color controlShadow; public static Color controlHighlight; public static Color controlBlack; public static Color controlWhite; public static Color scrollbarTrack; public static Color toolTip; private static final Insets GROOVE_INSETS; private static final Insets ETCHED_INSETS; static { control = Color.lightGray; controlShadow = Color.gray; controlHighlight = Color.white; controlBlack = Color.black; controlWhite = Color.white; scrollbarTrack = new Color(224, 224, 224); toolTip = new Color(255, 255, 225); GROOVE_INSETS = new Insets(2, 2, 2, 2); ETCHED_INSETS = new Insets(2, 2, 2, 2); } public static void drawBezel(Graphics g, int x, int y, int w, int h, boolean isPressed, boolean isDefault) { Color oldColor = g.getColor(); g.translate(x, y); if (isPressed) { if (isDefault) { g.setColor(controlBlack); g.drawRect(0, 0, w - 1, h - 1); } g.setColor(controlShadow); g.drawRect(1, 1, w - 3, h - 3); } else { if (isDefault) { g.setColor(controlBlack); g.drawRect(0, 0, w - 1, h - 1); g.setColor(controlHighlight); g.drawLine(1, 1, 1, h - 3); g.drawLine(2, 1, w - 4, 1); g.setColor(controlShadow); g.drawLine(2, h - 3, w - 3, h - 3); g.drawLine(w - 3, 1, w - 3, h - 4); g.setColor(Color.black); g.drawLine(1, h - 2, w - 2, h - 2); g.drawLine(w - 2, h - 2, w - 2, 1); } else { g.setColor(controlHighlight); g.drawLine(0, 0, 0, h - 1); g.drawLine(1, 0, w - 3, 0); g.setColor(controlShadow); g.drawLine(1, h - 2, w - 2, h - 2); g.drawLine(w - 2, 0, w - 2, h - 3); g.setColor(Color.black); g.drawLine(0, h - 1, w - 1, h - 1); g.drawLine(w - 1, h - 1, w - 1, 0); } g.translate(-x, -y); g.setColor(oldColor); } } public static void drawDashedRect(Graphics g, int x, int y, int width, int height) { for(int vx = x; vx < x + width; vx += 2) { g.drawLine(vx, y, vx, y); g.drawLine(vx, y + height - 1, vx, y + height - 1); } for(int vy = y; vy < y + height; vy += 2) { g.drawLine(x, vy, x, vy); g.drawLine(x + width - 1, vy, x + width - 1, vy); } } public static void drawEtchedRect(Graphics g, int x, int y, int w, int h) { Color oldColor = g.getColor(); g.translate(x, y); g.setColor(controlShadow); g.drawLine(0, 0, w - 1, 0); g.drawLine(0, 1, 0, h - 2); g.setColor(controlBlack); g.drawLine(1, 1, w - 3, 1); g.drawLine(1, 2, 1, h - 3); g.setColor(controlWhite); g.drawLine(w - 1, 0, w - 1, h - 1); g.drawLine(0, h - 1, w - 1, h - 1); g.setColor(control); g.drawLine(w - 2, 1, w - 2, h - 3); g.drawLine(1, h - 2, w - 3, h - 2); g.translate(-x, -y); g.setColor(oldColor); } public static void drawGroove(Graphics g, int x, int y, int w, int h) { Color oldColor = g.getColor(); g.translate(x, y); g.setColor(controlShadow); g.drawRect(0, 0, w - 2, h - 2); g.setColor(controlWhite); g.drawLine(1, h - 3, 1, 1); g.drawLine(1, 1, w - 3, 1); g.drawLine(0, h - 1, w - 1, h - 1); g.drawLine(w - 1, h - 1, w - 1, 0); g.translate(-x, -y); g.setColor(oldColor); } public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h) { g.setColor(controlBlack); g.drawLine(0, 0, 0, h - 1); g.drawLine(1, 0, w - 3, 0); g.setColor(controlShadow); g.drawLine(1, 1, 1, h - 2); g.drawLine(1, 1, w - 3, 1); g.setColor(controlHighlight); g.drawLine(0, h - 1, w - 1, h - 1); g.drawLine(w - 1, h - 1, w - 1, 0); } public static void drawString(Graphics g, String text, int underlinedChar, int x, int y) { char[] b = new char[1]; int index = -1; if (underlinedChar != 0) { b[0] = (char)underlinedChar; String s = (new String(b)).toUpperCase(); char uc = s.charAt(0); s = (new String(b)).toLowerCase(); char lc = s.charAt(0); int uci = text.indexOf(uc); int lci = text.indexOf(lc); if (uci == -1) { index = lci; } else if (lci == -1) { index = uci; } else { index = lci < uci ? lci : uci; } } g.drawString(text, x, y); if (index != -1) { FontMetrics fm = g.getFontMetrics(); Rectangle underlineRect = new Rectangle(); underlineRect.x = x + fm.stringWidth(text.substring(0, index)); underlineRect.y = y; underlineRect.width = fm.charWidth(text.charAt(index)); underlineRect.height = 1; g.fillRect(underlineRect.x, underlineRect.y + fm.getDescent() - 1, underlineRect.width, underlineRect.height); } } public static Insets getEtchedInsets() { return ETCHED_INSETS; } public static Insets getGrooveInsets() { return GROOVE_INSETS; } public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap) { if (((Container)b).getComponentCount() > 0) { return null; } else { Icon icon = b.getIcon(); String text = b.getText(); Font font = ((Component)b).getFont(); FontMetrics fm = ((Component)b).getToolkit().getFontMetrics(font); Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle viewR = new Rectangle(32767, 32767); SwingUtilities.layoutCompoundLabel(fm, text, icon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewR, iconR, textR, text == null ? 0 : textIconGap); Rectangle r = iconR.union(textR); Insets insets = ((JComponent)b).getInsets(); r.width += insets.left + insets.right; r.height += insets.top + insets.bottom; if (r.width % 2 == 0) { ++r.width; } if (r.height % 2 == 0) { ++r.height; } return r.getSize(); } } public static Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) { JMenuItem b = (JMenuItem)c; Icon icon = ((AbstractButton)b).getIcon(); String text = ((AbstractButton)b).getText(); KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText = acceleratorText + "+"; } acceleratorText = acceleratorText + KeyEvent.getKeyText(accelerator.getKeyCode()); } Font font = ((Component)b).getFont(); FontMetrics fm = ((Component)b).getToolkit().getFontMetrics(font); FontMetrics fmAccel = ((Component)b).getToolkit().getFontMetrics(UIManager.getFont("MenuItem.acceleratorFont")); Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle acceleratorR = new Rectangle(); Rectangle checkIconR = new Rectangle(); Rectangle arrowIconR = new Rectangle(); Rectangle viewR = new Rectangle(32767, 32767); layoutMenuItem(fm, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, ((AbstractButton)b).getVerticalAlignment(), ((AbstractButton)b).getHorizontalAlignment(), ((AbstractButton)b).getVerticalTextPosition(), ((AbstractButton)b).getHorizontalTextPosition(), viewR, iconR, textR, acceleratorR, checkIconR, arrowIconR, text == null ? 0 : defaultTextIconGap, defaultTextIconGap); Rectangle r = iconR.union(textR); boolean acceleratorTextIsEmpty = acceleratorText == null || acceleratorText.equals(""); if (!acceleratorTextIsEmpty) { r.width += acceleratorR.width; r.width += 7 * defaultTextIconGap; } r.width += checkIconR.width; r.width += 2 * defaultTextIconGap; r.width += 2 * defaultTextIconGap; r.width += arrowIconR.width; Insets insets = ((JComponent)b).getInsets(); r.width += insets.left + insets.right; r.height += insets.top + insets.bottom; if (r.width % 2 == 0) { ++r.width; } if (r.height % 2 == 0) { ++r.height; } return r.getSize(); } public static String layoutMenuItem(FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, Rectangle acceleratorR, Rectangle checkIconR, Rectangle arrowIconR, int textIconGap, int menuItemGap) { SwingUtilities.layoutCompoundLabel(fm, text, icon, verticalAlignment, horizontalAlignment, verticalTextPosition, horizontalTextPosition, viewR, iconR, textR, textIconGap); boolean acceleratorTextIsEmpty = acceleratorText == null || acceleratorText.equals(""); if (acceleratorTextIsEmpty) { acceleratorR.width = acceleratorR.height = 0; acceleratorText = ""; } else { acceleratorR.width = SwingUtilities.computeStringWidth(fmAccel, acceleratorText); acceleratorR.height = fmAccel.getHeight(); } if (checkIcon != null) { checkIconR.width = checkIcon.getIconWidth(); checkIconR.height = checkIcon.getIconHeight(); } else { checkIconR.width = checkIconR.height = 0; } if (arrowIcon != null) { arrowIconR.width = arrowIcon.getIconWidth(); arrowIconR.height = arrowIcon.getIconHeight(); } else { arrowIconR.width = arrowIconR.height = 0; } textR.x += checkIconR.width + menuItemGap; iconR.x += checkIconR.width + menuItemGap; Rectangle labelR = iconR.union(textR); acceleratorR.x += viewR.width - arrowIconR.width - menuItemGap - acceleratorR.width; acceleratorR.y = viewR.y + viewR.height / 2 - acceleratorR.height / 2; arrowIconR.x += viewR.width - arrowIconR.width; arrowIconR.y = viewR.y + labelR.height / 2 - arrowIconR.height / 2; checkIconR.y = viewR.y + labelR.height / 2 - checkIconR.height / 2; checkIconR.x += viewR.x; return text; } public static void paintMenuItem(Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) { JMenuItem b = (JMenuItem)c; ButtonModel model = ((AbstractButton)b).getModel(); Dimension size = ((Component)b).getSize(); Insets i = c.getInsets(); Rectangle viewRect = new Rectangle(size); viewRect.x += i.left; viewRect.y += i.top; viewRect.width -= i.right + viewRect.x; viewRect.height -= i.bottom + viewRect.y; Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); Rectangle acceleratorRect = new Rectangle(); Rectangle checkRect = new Rectangle(); Rectangle arrowRect = new Rectangle(); Font holdf = g.getFont(); Font f = ((Component)c).getFont(); g.setFont(f); FontMetrics fm = g.getFontMetrics(f); FontMetrics fmAccel = g.getFontMetrics(UIManager.getFont("MenuItem.acceleratorFont")); Color holdc = g.getColor(); if (c.isOpaque()) { if (!model.isArmed() && (!(c instanceof JMenu) || !model.isSelected())) { g.setColor(((Component)b).getBackground()); g.fillRect(0, 0, size.width, size.height); } else { g.setColor(background); g.fillRect(0, 0, size.width, size.height); } g.setColor(holdc); } KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText = acceleratorText + "+"; } acceleratorText = acceleratorText + KeyEvent.getKeyText(accelerator.getKeyCode()); } String text = layoutMenuItem(fm, ((AbstractButton)b).getText(), fmAccel, acceleratorText, ((AbstractButton)b).getIcon(), checkIcon, arrowIcon, ((AbstractButton)b).getVerticalAlignment(), ((AbstractButton)b).getHorizontalAlignment(), ((AbstractButton)b).getVerticalTextPosition(), ((AbstractButton)b).getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkRect, arrowRect, ((AbstractButton)b).getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap); if (checkIcon != null) { if (!model.isArmed() && (!(c instanceof JMenu) || !model.isSelected())) { g.setColor(((Component)b).getForeground()); } else { g.setColor(foreground); } checkIcon.paintIcon(c, g, checkRect.x, checkRect.y); g.setColor(holdc); } if (((AbstractButton)b).getIcon() != null) { Icon icon; if (!model.isEnabled()) { icon = ((AbstractButton)b).getDisabledIcon(); } else if (model.isPressed() && model.isArmed()) { icon = ((AbstractButton)b).getPressedIcon(); if (icon == null) { icon = ((AbstractButton)b).getIcon(); } } else { icon = ((AbstractButton)b).getIcon(); } icon.paintIcon(c, g, iconRect.x, iconRect.y); } if (text != null && !text.equals("")) { if (!model.isEnabled()) { if (UIManager.get("MenuItem.disabledForeground") instanceof Color) { g.setColor(UIManager.getColor("MenuItem.disabledForeground")); drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); } else { g.setColor(((Component)b).getBackground().brighter()); drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); g.setColor(((Component)b).getBackground().darker()); drawString(g, text, model.getMnemonic(), textRect.x - 1, textRect.y + fm.getAscent() - 1); } } else { if (!model.isArmed() && (!(c instanceof JMenu) || !model.isSelected())) { g.setColor(((Component)b).getForeground()); } else { g.setColor(foreground); } drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); } } if (acceleratorText != null && !acceleratorText.equals("")) { g.setFont(UIManager.getFont("MenuItem.acceleratorFont")); if (!model.isEnabled()) { if (UIManager.get("MenuItem.disabledForeground") instanceof Color) { g.setColor(UIManager.getColor("MenuItem.disabledForeground")); drawString(g, acceleratorText, model.getMnemonic(), acceleratorRect.x, acceleratorRect.y + fm.getAscent()); } else { g.setColor(((Component)b).getBackground().brighter()); drawString(g, acceleratorText, model.getMnemonic(), acceleratorRect.x, acceleratorRect.y + fm.getAscent()); g.setColor(((Component)b).getBackground().darker()); drawString(g, acceleratorText, model.getMnemonic(), acceleratorRect.x - 1, acceleratorRect.y + fm.getAscent() - 1); } } else { if (!model.isArmed() && (!(c instanceof JMenu) || !model.isSelected())) { g.setColor(UIManager.getColor("MenuItem.acceleratorForeground")); } else { g.setColor(UIManager.getColor("MenuItem.acceleratorPressedForeground")); } drawString(g, acceleratorText, model.getMnemonic(), acceleratorRect.x, acceleratorRect.y + fm.getAscent()); } } if (arrowIcon != null) { if (model.isArmed() || c instanceof JMenu && model.isSelected()) { g.setColor(foreground); } if (!(((Component)b).getParent() instanceof JMenuBar)) { arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y); } } g.setColor(holdc); g.setFont(holdf); } }